package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.drawers;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.GameFrame;
import ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.dialogs.NumberOfShipsDialog;
import ch.unisi.inf.pfii.teamwhite.risk.sound.SoundEngine;

public final class GalaxyDrawer {
	
	private Game game;
	
	private GameFrame gameFrame;
	private JLabel selection;
	
	private ArrayList<DrawerListener> listeners;

	public GalaxyDrawer(final Game game, final GameFrame gameFrame) {
		this.game = game;
		
		this.gameFrame = gameFrame;
		
		listeners = new ArrayList<DrawerListener>();
	}
	
//	This method creates a label which contains the number of ships the building and the image of the planet
	public final void drawGalaxy(final String[] line, final JPanel imagePanel) {
		
		int radius = 31;
		int x = Integer.parseInt(line[1]) - radius; 
		int y = Integer.parseInt(line[2]) - radius;
		
		final JLabel planet = new JLabel();
		planet.setBounds(x, y, radius*2, radius*2);
		
		planet.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1) {
					if (game.getCurrentPlayer().isDuringAssignment()) {
						if (game.getCurrentPlayer().getType().equals("Human")) {
							((Human) game.getCurrentPlayer()).selectPlanet(game.getGalaxy().getPlanets(), line[0]);
							((Human) game.getCurrentPlayer()).assignPlanets(game.getGalaxy().getPlanets());
						}
					} else {
						//game.checkPlayerState();
						if (game.getCurrentPlayer().getType().equals("Ai")) {
						} else {
							((Human) game.getCurrentPlayer()).selectPlanet(game.getGalaxy().getPlanets(), line[0]);
							if (((Human) game.getCurrentPlayer()).getSelectedPlanet() == null) {
								JOptionPane.showMessageDialog(imagePanel, "Planet Not Available!");
							} else {
								new SoundEngine("selection.wav", gameFrame.soundIsEnabled(), false);
								firePackRequired();
							}
						}

					}
				} else if (e.getButton() == 3) {
					if(game.getCurrentPlayer().getType().equals("Human") && ((Human)game.getCurrentPlayer()).getSelectedPlanet() != null &&((Human)game.getCurrentPlayer()).getSelectedPlanet().getShips().size() > 1){
						new SoundEngine("acknowledged.wav", gameFrame.soundIsEnabled(), false);
						new NumberOfShipsDialog(game, line[0], gameFrame);
					}
					
				}
			}
		});
		imagePanel.add(planet);
	}
	
	public final void addDrawerListener(final DrawerListener li){
		listeners.add(li);
	}
	
	public final void removeDrawerListener(final DrawerListener li){
		listeners.remove(li);
	}
	
	private final void firePackRequired(){
		for (final DrawerListener li : listeners) {
			li.packRequired();
		}
	}
}
